package com.example.zg3.notice

import androidx.paging.PageKeyedDataSource
import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.example.base.entity.NoticeBeanItem
import com.example.base.net.ApiService

class NoticeDataSource(val apiService: ApiService):PagingSource<Int,NoticeBeanItem>(){
    override fun getRefreshKey(state: PagingState<Int, NoticeBeanItem>): Int? {
        return null
    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, NoticeBeanItem> {
        try {
            val startPage = params.key ?: 0
            val notice = apiService.getNotice(startPage, 10)
            val nextPage = if (notice.data.size==10) startPage+1 else null
            return LoadResult.Page(notice.data,null,nextPage)
        }catch (e:Exception){
            return LoadResult.Error(e)
        }
    }

}